#ifndef MESSAGESHOWAREA_H
#define MESSAGESHOWAREA_H

#include <QWidget>
#include <QLabel>

#include "model/data.h"
using model::Message;

class QScrollArea;

class MessageShowArea : public QWidget
{
    Q_OBJECT
public:
    MessageShowArea();
    void addMessage(bool isLeft, const Message& message);
    void addFrontMessage(bool isLeft, const Message& message);
    void clear();

    void scrollToEndLater(int laterTime = 200);

private:
    QScrollArea* scrollArea;
    QWidget* container;
};


////////// 消息的最小单元对象
class MessageItem : public QWidget
{
    Q_OBJECT
public:
    // 通过参数来区分构造左右侧消息
    MessageItem(bool isLeft);

    // 工厂模式构造对象
    static MessageItem* makeMessageItem(bool isLeft, const Message& message);

private:
    static QWidget* makeTextMessageItem(const QByteArray& content, bool isLeft);
    static QWidget* makeImageMessageItem(const QString& fileId, const QByteArray& content, bool isLeft);
    static QWidget* makeSoundMessageItem(const QString& fileId, const QByteArray& content, bool isLeft);
    static QWidget* makeFileMessageItem(const QString& fileId, const QByteArray& content,
                                        bool isLeft, const QString& fileName);
private:
    bool isLeft;
};


//////////// 文本消息的正文数据对象
class MessageContentLabel : public QWidget
{
    Q_OBJECT
public:
    MessageContentLabel(const QString& text, bool isLeft);

protected:
    void paintEvent(QPaintEvent* event);

private:
    QLabel* label;
    bool isLeft;
};

//////////// 图片消息的正文数据对象
class MessageImageLabel : public QWidget
{
    Q_OBJECT
public:
    MessageImageLabel(const QString& fileId, const QByteArray& content, bool isLeft);

private:
    void getSingleFileDoneHandle(const QString& fileId, const QByteArray& fileContent);

    void loadImage(const QByteArray& imageData);

    void updateImageSize();

private:
    QPushButton* imageBtn;
    QString fileId;
    QByteArray content;     // 图片的二进制数据
    bool isLeft;

    QImage image;
    QSize imageSize;

    bool imageLoaded = false;
};


//////////// 文件消息的正文数据对象
class MessageFileLabel : public QWidget
{
    Q_OBJECT
public:
    MessageFileLabel(const QString& fileId, const QByteArray& content, const QString& fileName, bool isLeft);

protected:
    void paintEvent(QPaintEvent* event);
    void mousePressEvent(QMouseEvent* event);

private:
    void getSingleFileDoneHandle(const QString& fileId, const QByteArray& fileContent);

    void saveFileToLocal();

private:
    QString fileId;
    QString fileName;
    QByteArray content;     // 文件的二进制数据
    bool isLeft;

    QLabel* label;
    bool loadContentDone  = false;
};

//////////// 语音消息的正文数据对象
class MessageSpeechLabel : public QWidget
{
    Q_OBJECT
public:
    MessageSpeechLabel(const QString& fileId, const QByteArray& content, bool isLeft);

protected:
    void paintEvent(QPaintEvent* event);
    void mousePressEvent(QMouseEvent* event);
    void contextMenuEvent(QContextMenuEvent* event);

private:
    void getSingleFileDoneHandle(const QString& fileId, const QByteArray& fileContent);
    void speechPlayDoneHandle();
    void speechConvertTextDoneHandle(const QString& fileId, const QString& result);

private:
    QString fileId;
    QByteArray content;     // 图片的二进制数据
    bool isLeft;

    QLabel* label;
    bool loadContentDone = false;
};


#endif // MESSAGESHOWAREA_H
